package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.model.Country;

@Repository
public class CountryDaoImpl {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public void addCountry(Country country) {
		sessionFactory.getCurrentSession().save(country);
	}

	@SuppressWarnings("unchecked")
	public List<Country> listCountry() {

		return sessionFactory.getCurrentSession().createQuery("from Country as c order by c.id")
				.list();
	}

	public void removeCountry(Integer id) {
		Country country = (Country) sessionFactory.getCurrentSession().load(
				Country.class, id);
		if (null != country) {
			sessionFactory.getCurrentSession().delete(country);
		}

	}

	@SuppressWarnings("unchecked")
	public Country getCountryById(Integer id) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Country where id=  :id");
		 query.setParameter("id", id);
		 List<Country>countries  = new ArrayList<Country>();
		 countries =   query.list();
		 if(countries.size() >0){
			 return countries.get(0);
		 }else{
			 return null;
		 }
			
		
	}
	@SuppressWarnings("unchecked")
	public Country getCountryByName(String  name) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Country where countryName=  :name");
		 query.setParameter("name", name);
		 List<Country>countries  = new ArrayList<Country>();
		 countries =   query.list();
		 if(countries.size() >0){
			 return countries.get(0);
		 }else{
			 return null;
		 }
			
		
	}
}
